export default {
	data() {
		return {
			userInfo: this.$store.state.login || {},
			couponList: []
		}
	},
	async onLoad(options) {
		console.log("vip index onLoad: ", options);
		await this.getVipCouponList();
	},
	async onShow() {},
	onHide() {},
	methods: {
		async getVipCouponList() {
			const getRes = await this.$uniCloudCallFunction({
				name: "xxf-coupon",
				action: "getVipCouponList",
				data: {
					uid: this.$store.state.login.uid,
				}
			});
			console.log("getRes: ", getRes);
			if (getRes) {
				this.couponList = getRes.list;
			}
		},
		async joinVip() {
			console.log('准备开通会员！');
			const putRes = await this.$uniCloudCallFunction({
				name: 'xxf-vip',
				action: 'putOpenVipOrder',
				data: {
					uid: this.$store.state.login.uid,
					totalMoney: 990,
					discount: 0,
					needMoney: 990
				}
			});
			console.log('putRes: ', putRes);
			if (putRes) {
				// 支付
				await this.onPaying(putRes);
			}
		},
		async onPaying(orderOpt) {
			console.log('开通会员支付！', orderOpt);
			if (orderOpt._id) {
				const payRes = await this.$uniCloudCallFunction({
					name: 'xxf-vip',
					action: "payOpenVipOrder",
					data: {
						uid: this.$store.state.login.uid,
						openId: this.$store.state.login.openId,
						orderId: orderOpt._id
					}
				});
				console.log('payRes:', payRes);
				const self = this;
				if (payRes) {
					uni.requestPayment({
						timeStamp: payRes.timeStamp,
						nonceStr: payRes.nonceStr,
						package: payRes.package, //统一下单接口返回的 prepay_id 格式如：prepay_id=***
						signType: payRes.signType || 'MD5',
						paySign: payRes.paySign, //签名
						success(res) {
							console.log("支付成功", res)
							self.$utils.showToast(self, {
								message: '开通成功！',
								mode: 1
							});
							setTimeout(() => {
								self.afterOpenVip();
							}, 2500);
						},
						fail(res) {
							console.log("支付失败", res)
							self.$utils.showToast(self, {
								message: '开通失败！',
								mode: 2
							});
						},
						complete(res) {
							console.log("支付完成", res);
							try {
								self.$uniCloudCallFunction({
									name: 'xxf-vip',
									action: "checkOpenVipOrderStatus",
									data: {
										uid: self.$store.state.login.uid,
										openId: self.$store.state.login.openId,
										orderId: orderOpt._id
									}
								});
							} catch (e) {
								console.log("complete checkOrderStatus", e);
							}
						}
					})
				} else {
					self.$utils.showToast(self, {
						message: '开通失败！',
						mode: 2
					});;
				}
			}
		},
		async afterOpenVip() {
			const vipRes = await this.$uniCloudCallFunction({
				name: 'xxf-user',
				action: 'toBeVip',
				data: {
					uid: this.$store.state.login.uid,
				}
			});
			console.log('vipRes: ', vipRes);
			if (vipRes && vipRes._id) {
				this.$store.commit("login/setLoginInfo", {
					role: vipRes.role,
				});
				this.userInfo = this.$store.state.login || {};
			}
		},
		async toReceived(e) {
			console.log("toReceived: ", e);
			const receiveRes = await this.$uniCloudCallFunction({
				name: 'xxf-coupon',
				action: 'receiveVipCoupon',
				data: {
					uid: this.$store.state.login.uid,
					coupon_id: e._id,
					type: e.type
				}
			});
			console.log('receiveRes: ', receiveRes);
			if (receiveRes) {
				await this.$utils.showToast(this, {
					message: '领取成功',
					mode: 1,
				})
				await this.getVipCouponList();
			}
		}
	}
}